package com.tssm.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.tssm.common.dao.impl.BaseDAOImpl;
import com.tssm.core.util.Pages;
import com.tssm.dao.SarcQuotationCargoDAO;
import com.tssm.pojo.SarcQuotationCargo;
@Repository("quotationCargoDAO")
public class SarcQuotationCargoDAOImpl extends BaseDAOImpl<SarcQuotationCargo> implements SarcQuotationCargoDAO {
	
	@SuppressWarnings("unchecked")
	public List<SarcQuotationCargo> getQuotationCargoList(final Long id, final Pages pages) {
		return (List<SarcQuotationCargo>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session) throws SQLException, HibernateException {
						String hql = "from SarcQuotationCargo squocargo where squocargo.quotationid="+id+" and squocargo.dr=0 order by squocargo.quocargoid desc";
						Query query = session.createQuery(hql);
						if(pages != null)
							query.setFirstResult(pages.getFirstIndex()).setMaxResults(pages.getPageSize());
						return query.list();
					}
				});
	}
	
	public int getQuotationCargoListCount(final Long id) {
		return (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws SQLException, HibernateException {
						String hql = "select count(squocargo.quocargoid) from SarcQuotationCargo squocargo where squocargo.quotationid="+id+" and squocargo.dr=0";						
						Query query = session.createQuery(hql);
						Long l = (Long) query.uniqueResult();
						return l.intValue();
					}
				});
	}
	
	public SarcQuotationCargo getQuotationCargoById(final Long id) {
		SarcQuotationCargo squocargo = null;
		String hql = "from SarcQuotationCargo squocargo where squocargo.quocargoid=" + id.toString() + " and squocargo.dr=0";
		List<SarcQuotationCargo> squocargolist = super.find(hql);
		if(squocargolist.size() > 0) squocargo = squocargolist.get(0);
		return squocargo;		
	}

}
